PROGRAM MAIN
  USE GLOBVAR
  USE INIT
  USE DATAMM 
  USE smm
  USE smm2
  !USE smmboot
  USE NRUTIL
  USE MATRIX
  USE PROBABILITY
  USE ars 
  USE ANNEALING
  !USE weight
  !USE LIKELIHOOD
  USE RANDOM
  USE MINIMIZATION,ONLY: BFGS
  USE SIMPLEX
  USE STATISTICS
  !USE boots
  
  !USE MPI
  
IMPLICIT NONE
  REAL(8) :: theta(N_var),bound(N_var,2),theta_initial(N_var)
  REAL(8) :: mu,sig,aa,mm(n_mom),aa2,momm(n_boot,n_mom)!,winv(n_mom2,n_mom2)
  LOGICAL :: check
  
  INTEGER :: j,n,ierr,hz,tin,tout,k,i,jj,kk
  myid=0

  CALL INITIALIZE()
  CALL datam(mm)
  moment=mm
   OPEN(6424,file="data.out")
    DO i = 1,N_mom 
      WRITE(6424,'(200F16.8)') moment(i)
    END DO
   CLOSE(6424)
   !stop
  theta=0.0d0
  !kk=n_x_l0+3*N_levelf+n_x_eta+2
  !theta(kk+1:n_var)=0.0d0
  !theta(kk+n_levelf+1)=1.0d0

                OPEN(1,file="initial.out")
                    DO j=1,SIZE(thetafix)
                       READ(1,fmt=*) thetafix(j)
                    END DO
                CLOSE(1)



                OPEN(1,file="point.out")
                    DO j=1,SIZE(theta)
                       READ(1,fmt=*) theta(j)
                    END DO
                CLOSE(1)
        aa=mom(theta)
        aa2=mom2(theta)
        !theta=theta_true!*0.90d0
       IF (myid==0) write(*,*) aa,aa2
         stop
      !CALL BOOT(momm)
      !momentboot=momm

      
              DO j=1,1
                IF (INT(J/11.0D0)==1) THEN
                          CALL bfgs(theta,1.0d-6,1.0d-6,mom,0,.False.) 
                          CALL Nelder_Meade(theta,1.0d-8,mom,0,2000)
                          CALL Nelder_Meade(theta,1.0d-8,mom,0,2000)
                          CALL Nelder_Meade(theta,1.0d-8,mom,0,2000)
                          CALL Simulated_Annealing(50.0d0,0.80d0,30,theta,mom,150,0,(/1234,6284,123656+j,231365488/))
                          CALL Accelerated_Random_Search(theta,0.5d0,bound,50,100,mom,.TRUE.,0,(/1,2,3,4+j/)) 
                          CALL Nelder_Meade(theta,1.0d-8,mom,0,2000)
              
                        DO i=1,N_var 
                           bound(i,1)=max(theta(i)*0.1, theta(i)-1.0d0)
                           bound(i,2)=min(theta(i)*1.8, theta(i)+1.0d0)
                        END DO

                          CALL Accelerated_Random_Search(theta,0.5d0,bound,50,100,mom,.TRUE.,0,(/1,2+j,3,4/)) 
                          CALL Nelder_Meade(theta,1.0d-7,mom,0,1500)
                          CALL Simulated_Annealing(50.0d0,0.50d0,30,theta,mom,150,0,(/1234,6284+j,123656,231365488/))
                         CALL Nelder_Meade(theta,1.0d-7,mom,0,1500)
               ELSE
                          CALL Nelder_Meade(theta,1.0d-8,mom2,0,2000)
                          CALL Nelder_Meade(theta,1.0d-8,mom2,0,2000)
                          CALL Nelder_Meade(theta,1.0d-8,mom2,0,2000)  
                          CALL bfgs(theta,1.0d-4,1.0d-5,mom2,0,.False.)                      
                          CALL Simulated_Annealing(50.0d0,0.80d0,30,theta,mom2,150,0,(/1234+j,6284,123656,231365488/))                  
                          CALL Accelerated_Random_Search(theta,0.5d0,bound,50,100,mom2,.TRUE.,0,(/1+j,2,3,4/)) 
                          CALL Nelder_Meade(theta,1.0d-8,mom2,0,1500)
                        DO i=1,N_var 
                           bound(i,1)=max(theta(i)*0.1, theta(i)-1.0d0)
                           bound(i,2)=min(theta(i)*1.8, theta(i)+1.0d0)
                        END DO
                       CALL Accelerated_Random_Search(theta,0.5d0,bound,50,100,mom2,.TRUE.,0,(/1,2,3,4+j/)) 
                       CALL Nelder_Meade(theta,1.0d-7,mom2,0,1500)
                       CALL Simulated_Annealing(50.0d0,0.50d0,30,theta,mom2,150,0,(/1234,6284,123656+j,231365488/))
                       CALL Nelder_Meade(theta,1.0d-7,mom2,0,1500)
                       CALL Nelder_Meade(theta,1.0d-8,mom2,0,2000)
                       CALL Nelder_Meade(theta,1.0d-8,mom2,0,2000)
                       CALL Nelder_Meade(theta,1.0d-8,mom2,0,2000)
                  END IF
              END DO


  
END PROGRAM MAIN
